package com.hlcppmm.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

public class ServiceManager {

	private Map<String, ServiceDefinition> mapOfServices = new ConcurrentHashMap<String, ServiceDefinition>();
	private Map<String, Boolean> enabledServices = new ConcurrentHashMap<String, Boolean>();
	
	protected void add(String serviceIdentifier, ServiceDefinition serviceDefinition) 
	{
		mapOfServices.put(serviceIdentifier, serviceDefinition);
		enabledServices.put(serviceIdentifier, true);
	}
	
	protected void update(String serviceIdentifier, ServiceDefinition serviceDefinition) 
	{
		mapOfServices.put(serviceIdentifier, serviceDefinition);
	}

	protected void remove(String serviceIdentifier) 
	{
		mapOfServices.remove(serviceIdentifier);
		enabledServices.remove(serviceIdentifier);
	}
	
	protected void disable(String serviceIdentifier) 
	{
		enabledServices.put(serviceIdentifier, false);
	}
	
	protected void enable(String serviceIdentifier) 
	{
		enabledServices.put(serviceIdentifier, true);
	}
	
	protected Collection<ServiceDefinition> getAll()
	{
		return mapOfServices.values();
	}
	
	protected ServiceDefinition get(String serviceIdentifier)
	{
		return mapOfServices.get(serviceIdentifier);
	}

	protected Collection<ServiceDefinition> getEnabled()
	{
		Collection<ServiceDefinition> list = new ArrayList<ServiceDefinition>();
		for (Entry<String, Boolean> entry: enabledServices.entrySet()) 
		{
			if(entry.getValue())
			{
				list.add(mapOfServices.get(entry.getKey()));
			}
		}
		return list;
	}
	
	protected Collection<ServiceDefinition> getDisabled()
	{
		Collection<ServiceDefinition> list = new ArrayList<ServiceDefinition>();
		for (Entry<String, Boolean> entry: enabledServices.entrySet()) 
		{
			if(!entry.getValue())
			{
				list.add(mapOfServices.get(entry.getKey()));
			}
		}
		return list;
	}
}
